package com.Utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.listener.FlightExcelLisener;
import com.listener.TempRouteLisener;
import com.pojo.FlightExcel;
import com.pojo.RouteSchedule;
import com.pojo.TempRoute;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @ClassName RouteTransfers
 * @Author lenovo
 * @Date 2023/12/1 17:10
 * @Version 1.0
 * @Description 航线转换
 **/
public class RouteTransfers {
    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\lenovo\\Desktop\\航线表\\";
        File file = new File(filePath);
        //用数组把文件夹下的文件存起来
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile()) {
                String name = file1.getName();
                System.out.println(name);
                TempRouteLisener routeListener = new TempRouteLisener();
                routeListener.setName(name.replace("。","-").replaceAll(".csv",""));
                ExcelReader excelReader = EasyExcel.read(filePath + name, TempRoute.class, routeListener).build();
                ReadSheet readSheet = EasyExcel.readSheet(0).build();
                excelReader.read(readSheet);
                excelReader.finish();
            }
        }
        List<RouteSchedule> routeSchedules = TempRouteLisener.getRouteSchedules();

        OutputStream outputStream = Files.newOutputStream(Paths.get("user1.xlsx"));
        // 创建 EasyExcel 的 ExcelWriterBuilder
        ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(outputStream);
        excelWriterBuilder
                .sheet("Sheet1")
                .useDefaultStyle(false)
                .doWrite(routeSchedules);
        System.out.println("Excel 文件已创建成功！");
        String currentDirectory = System.getProperty("user.dir");
        System.out.println("当前工作目录：" + currentDirectory);
    }
}
